package com.huopaolan.yohuoshop.Mvp.Presenter;

import android.util.Log;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.BaseEntity;
import com.huopaolan.yohuoshop.Bean.CommodityDetailsEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.CommodityDetaContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CommodityDetaPresenter extends BasePresenter<CommodityDetaContract.CommodityDetaIView, CommodityDetaContract.CommodityDetaIModel> {

    @Inject
    public CommodityDetaPresenter(CommodityDetaContract.CommodityDetaIView commodityDetaIView, CommodityDetaContract.CommodityDetaIModel commodityDetaIModel) {
        super(commodityDetaIView, commodityDetaIModel);
    }

    /**
     * 商品请求
     */
    public void GoodReuqest() {
        JSONObject good = new JSONObject();
        try {
            good.put("goodsid", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = good.toString();
        m.commodity_deta_request(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommodityDetailsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommodityDetailsEntity entity) {
                        if (entity == null) {
                            return;
                        }
                        v.CommodityDetaSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //添加商品到购物车
    public void AddCarRequest(String userid, String goodsid, String shopname, String shopcolor, String shopsize, String shopnum, String shopprice) {
        JSONObject car = new JSONObject();
        try {
            car.put("userid", userid);
            car.put("goodsid", goodsid);
            car.put("shopname", shopname);
            car.put("shopcolor", shopcolor);
            car.put("shopsize", shopsize);
            car.put("shopnum", shopnum);
            car.put("shopprice", shopprice);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = car.toString();
        m.add_car_request(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity entity) {
                        v.CommodityAddCarSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        Log.d("AddCar", "UpdateCreate=======>Completed");
                    }
                });
    }
}
